/*!
 * @file median.h
 * @author Yasutomo Kawanishi
 * @date Last Change: 2011/Feb/23.
 * */
#ifndef __BACKGROUND_IMAGE_GENERATOR_MDIAN__
#define __BACKGROUND_IMAGE_GENERATOR_MDIAN__
#include "common.h"
#include <vector>
#include <deque>

class BackgroundImageGeneratorMedian{
public:
		BackgroundImageGeneratorMedian(int capacity=30);
		~BackgroundImageGeneratorMedian();
		BackgroundImageGeneratorMedian(const BackgroundImageGeneratorMedian& other);
		const BackgroundImageGeneratorMedian& operator=(const BackgroundImageGeneratorMedian& other);

		void generate(const cv::Mat& src, cv::Mat& dst);
		void update(const cv::Mat& src);
		void train(const std::vector<cv::Mat>& src);

		void save(const std::string& _path)const;
		void load(const std::string& _path);
private:
			unsigned char capacity;
			int indexNew;
			bool isInit;
			std::vector<cv::Mat> arrImg;
			unsigned char *arrSort;
			int height;
			int width;
			int ch;
			int size;
			cv::Mat previousUpdate;

			void sort1Table( unsigned char *table, unsigned char **arrCol );
			void initialize(const cv::Mat& img);
};
#endif